package com.jing.taobao.controller.user;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.jing.system.comm.BaseController;
import com.jing.system.comm.enums.GenderType;
import com.jing.taobao.pojo.user.TaobaoUser;
import com.jing.taobao.pojo.user.TaobaoWeibo;
import com.jing.taobao.service.user.TaobaoWeiboService;
import com.jing.taobao.service.user.TbUserService;
import com.jing.weibo.pojo.user.UserInfo;
import com.taobao.api.ApiException;
import com.taobao.api.domain.User;
import com.taobao.tools.UserApi;

/**
 * 淘宝用户登录的Controller
 * @author jing.yue
 * @version 2012/08/29 1.0.0
 */
@Controller
public class TbUserController extends BaseController {

	private static final Logger logger = Logger.getLogger(TbUserController.class);

	@Autowired
	private TbUserService tbUserService;
	@Autowired
	private TaobaoWeiboService taobaoWeiboService;

	/**
	 * 跳转到推广页
	 * @return
	 */
	@RequestMapping("/tbSpreadMain")
	public String tbSpreadMain(HttpServletRequest request, ModelMap modelMap) {
		return "taobao/spreadMain";
	}

	/**
	 * 淘宝授权后访问的页面
	 * @param request
	 * @param modelMap
	 * @return
	 */
	@RequestMapping("/taobaoMain")
	public String taobaoMain(HttpServletRequest request, ModelMap modelMap, String top_session) {
		//top_session为淘宝的sessionKey
		logger.info("sessionKey为: " + top_session);
		try {
			//获取淘宝用户卖家信息
			User user = UserApi.getSeller(top_session, null);
			if(user == null) {
				logger.error("用户信息不能为空!");
				return ERROR;
			}
			TaobaoUser tbUser = new TaobaoUser();
			//用户名
			tbUser.setUsername(String.valueOf(user.getUserId()));
			//昵称
			tbUser.setNickname(user.getNick());
			//性别
			tbUser.setGender(GenderType.getCodeByText(user.getSex()));
			//图像地址
			tbUser.setAvatar(user.getAvatar());
			//是否开过店铺[true是/false否]
			tbUser.setHasshop(user.getHasShop().toString());

			//是否存在记录
			if(tbUserService.findByUsername(tbUser.getUsername()) != null) {
				tbUserService.updateTaobaoUser(tbUser);
			} else {
				tbUserService.saveTaobaoUser(tbUser);
			}
			setSessionTaobaoUser(request, tbUser);
		} catch (ApiException e) {
			logger.error(e.getMessage(), e);
			return ERROR;
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			return ERROR;
		}
		return "redirect:tbSpreadMain.thtml";
	}

	/**
	 * 跳转到淘宝微博用户登录后回调的方法
	 * @param request
	 * @param modelMap
	 * @return
	 */
	@RequestMapping("/toTaobaoWeiboLogin")
	public String toTaobaoWeiboLogin(HttpServletRequest request, ModelMap modelMap) {
		setSession(request, SESSION_LOGIN_TYPE, "taobaoWeibo");
		return "redirect:taobao/taobaoWeiboLogin.jsp";
	}

	/**
	 * 淘宝微博用户登录后回调的方法
	 * @param request
	 * @param modelMap
	 * @return
	 */
	@RequestMapping("/taobaoWeibo")
	public String taobaoWeibo(HttpServletRequest request, ModelMap modelMap) {
		try {
			UserInfo userInfo = getSessionUser(request);
			if(userInfo == null) {
				return "redirect:toTaobaoWeiboLogin.thtml";
			}
			TaobaoUser taobaoUser = getSessionTaobaoUser(request);
			TaobaoWeibo taobaoWeibo = new TaobaoWeibo();
			taobaoWeibo.setTaobaousername(taobaoUser.getUsername());
			taobaoWeibo.setWeibousername(userInfo.getUsername());
			//处理绑定淘宝和微博的用户信息[绑定前需要判断是否绑定过]
			if(taobaoWeiboService.findByTbWbUsername(taobaoWeibo) == null) {
				taobaoWeiboService.saveTaobaoWeibo(taobaoWeibo);
			}
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			return ERROR;
		}
		return "taobao/result/successLogin";
	}

	/**
	 * 退出
	 * @param request
	 * @return
	 */
	@RequestMapping("/taobaoExit")
	public String exit(HttpServletRequest request) {
		try {
			TaobaoUser taobaoUser = getSessionTaobaoUser(request);
			if(taobaoUser != null) {
				removeSessionTaobaoUser(request);
			}
		} catch (Exception e) {
			logger.error(e.getMessage());
		}
		return "redirect:taobao/index.jsp";
	}

}